package com.core.tool.generate;

import java.util.*;

import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import freemarker.template.Template;

import com.core.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TemplateTool {
	private static Log log = LogFactory.getLog(TemplateTool.class);
	private FreeMarkerConfigurer freeMarkerConfigurer;

	public boolean createStaticFile(String templateName, Map data, String url) {
		boolean ret = false;
		try {
			if (templateName == null || data == null || url == null) {
				return ret;
			}

			Template template = freeMarkerConfigurer.getConfiguration()
					.getTemplate(templateName);

			String context = FreeMarkerTemplateUtils.processTemplateIntoString(
					template, data);

			ret = FileUtil.writeFileEncoding(url, context, FileUtil.UTF8);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public FreeMarkerConfigurer getFreeMarkerConfigurer() {
		return freeMarkerConfigurer;
	}

	public void setFreeMarkerConfigurer(
			FreeMarkerConfigurer freeMarkerConfigurer) {
		this.freeMarkerConfigurer = freeMarkerConfigurer;
	}

}
